package com.zzsy.carosshopprojects.activitys.home.mainpage.member.memberconsum.order;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import com.yatoooon.screenadaptation.ScreenAdapterTools;
import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.adapter.CouponListAdapter;
import com.zzsy.carosshopprojects.base.BaseFragment;
import com.zzsy.carosshopprojects.view.refreshview.SpacesItemDecoration;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * 优惠券选择列表
 */
public class CouponListFragment extends BaseFragment {
    public static CouponListFragment newInstance(String productId, String memberId, String payAmount, String memberCardId) {
        CouponListFragment couponListFragment = new CouponListFragment();
        Bundle bundle = new Bundle();
        bundle.putString("productId", productId);
        bundle.putString("memberId", memberId);
        bundle.putString("payAmount", payAmount);
        bundle.putString("memberCardId", memberCardId);
        couponListFragment.setArguments(bundle);
        return couponListFragment;
    }

    private CouponListMode couponListMode;
    private CouponListAdapter couponListAdapter;
    @BindView(R.id.recycleView)
    RecyclerView recycleView;

    @Override
    protected int initLayout() {
        return R.layout.fg_couponlist;
    }

    @Override
    protected void initView(View view) {
        initTitle(view, getResources().getString(R.string.CouponList), getActivity());
        couponListAdapter = new CouponListAdapter(getContext(), null, 0);
        couponListAdapter.setOnItemClickListener((v, position) -> {
            if (couponListAdapter.getListData().get(position).isShow()) {
                couponListAdapter.choseCopun(Integer.valueOf(getArguments().getString("payAmount")), position, couponListAdapter.getListData());
            }
        });
        recycleView.setLayoutManager(new LinearLayoutManager(context));
        int space = ScreenAdapterTools.getInstance().loadCustomAttrValue(20);
        recycleView.addItemDecoration(new SpacesItemDecoration(space));
        recycleView.setAdapter(couponListAdapter);
    }

    @Override
    protected void initData(Context mContext) {
        couponListMode = getViewModelProvider().get(CouponListMode.class);

    }

    @Override
    protected void loadMethod() {
        getData();
        setOkData();
    }

    @OnClick({R.id.tv_no_user, R.id.tv_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_no_user://不使用优惠券
                couponListMode.noUseCoupon();
                break;
            case R.id.tv_ok://完成
                couponListMode.setOkData(getContext(), couponListAdapter);
                break;
        }
    }

    private void getData() {
        couponListMode.getliveData().observe(this, couponUserdBeans -> {
            couponListAdapter.setListData(couponUserdBeans);
            couponListAdapter.notifyDataSetChanged();
        });
        couponListMode.getCouponList(getContext(), getArguments().getString("memberId"), getArguments().getString("productId"), getArguments().getString("memberCardId"), getArguments().getString("payAmount"));
    }

    /**
     * 优惠券选择完成回调
     */
    private void setOkData() {
        couponListMode.getlive().observe(this, couponChoseBean -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable("key", couponChoseBean);
            setFragmentResult(104, bundle);
            pop();
        });
    }
}
